clear
capture install outreg2
capture install ivcrc
macro drop _all

cd "[FILE_LOCATION]" // Change to appropriate file location
use CARD, clear

global R = 500 // Number of bootstrap replications
set seed 121212

label variable educ "Education 1976"
label variable lwage "Log(wage)"
label variable black "Black"
label variable south "Lives in South 1976"
label variable south66 "Lives in South 1966"
label variable smsa "Urban 1976"
label variable smsa66 "Urban 1966"
label variable exper "Experience"
label variable expersq "Experience$^2$"
label variable nearc4 "Lives close to 4 year college 1966"


capture program drop myboot
program define myboot, eclass
 preserve 
  bsample
  ivclp_bininstr lwage $controls , endog(educ = nearc4) fm novhat mat(init)
 //restore
end

capture program drop myboot2
program define myboot2, eclass
 preserve 
  bsample
  ivclp_bininstr lwage $controls , endog(educ = nearc4) fm novhat extra(`1') mat(init)
 restore
end

capture program drop ConvertVar	
program define ConvertVar, eclass
ereturn repost V = Var
end

// No controls 
global controls

regress lwage educ $controls
outreg2 using "Table1.xls", replace label noaster nocons ctitle("OLS") addtext(Controls, No) dec(4) keep(educ) excel
ivregress 2sls lwage (educ = nearc4) $controls
outreg2 using Table1.xls, append label noaster nocons ctitle("2SLS") addtext(Controls, No) dec(4) keep(educ)
ivcrc lwage (educ = nearc4) $controls , bootstrap(reps($R )) 
outreg2 using Table1.xls, append label noaster nocons ctitle("IVCRC") addtext(Controls, No) dec(4) keep(educ)
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4) novhat
gen v = 1
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP") addtext(Controls, No) dec(4) keep(educ)
drop v*
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4) fgm novhat
gen v = 1
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP-FGM") addtext(Controls, No) dec(4) keep(educ)
drop v*
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4)  ifgm novhat
gen v = 1
gen v2 = 1
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP-iFGM") addtext(Controls, No) dec(4) keep(educ)
drop v*

quietly reg educ nearc4 $controls
predict v, resid
quietly fmm 2, emopts(iterate(100)): regress v nearc4 
mat init = e(b)
drop v*
simulate b = _b[educ], reps($R ): myboot
sum b
scalar sd = r(sd)
use CARD, clear
label variable educ "Education 1976"
label variable lwage "Log(wage)"
label variable black "Black"
label variable south "Lives in South 1976"
label variable south66 "Lives in South 1966"
label variable smsa "Urban 1976"
label variable smsa66 "Urban 1966"
label variable exper "Experience"
label variable expersq "Experience$^2$"
label variable nearc4 "Lives close to 4 year college 1966"
ivclp_bininstr lwage $controls , endog(educ = nearc4) fm mat(init)
mat Var = e(V)
mat Var[1,1] = sd^2
ConvertVar
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP-FM(2)") addtext(Controls, No) dec(4) keep(educ)
capture drop v*

// Controls 
global controls black south south66 smsa smsa66 exper expersq

regress lwage educ $controls
outreg2 using Table1.xls, append label noaster nocons ctitle("OLS") addtext(Controls, Yes) dec(4) keep(educ)
ivregress 2sls lwage (educ = nearc4) $controls
outreg2 using Table1.xls, append label noaster nocons ctitle("2SLS") addtext(Controls, Yes) dec(4) keep(educ)
ivcrc lwage (educ = nearc4) $controls , bootstrap(reps($R )) // ADD BOOTSTRAP SE
outreg2 using Table1.xls, append label noaster nocons ctitle("IVCRC") addtext(Controls, Yes) dec(4) keep(educ)
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4) novhat
gen v = 1
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP") addtext(Controls, Yes) dec(4) keep(educ) 
drop v*
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4) fgm novhat
gen v = 1
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP-FGM") addtext(Controls, Yes) dec(4) keep(educ)
drop v*
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4) ifgm novhat
test v2 c.v2#c.nearc4 c.v2#c.black c.v2#c.south c.v2#c.south66 c.v2#c.smsa c.v2#c.smsa66 c.v2#c.exper c.v2#c.expersq c.educ#c.v2 c.educ#c.v2#c.nearc4 c.educ#c.v2#c.black c.educ#c.v2#c.south c.educ#c.v2#c.south66 c.educ#c.v2#c.smsa c.educ#c.v2#c.smsa66 c.educ#c.v2#c.exper c.educ#c.v2#c.expersq
scalar pval = r(pvalue)
gen v = 1
gen v2 = 1
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP-iFGM") addtext(Controls, Yes) dec(4) keep(educ)
drop v*

quietly reg educ nearc4 $controls
predict v, resid
quietly fmm 2, emopts(iterate(100)): regress v nearc4 
mat init = e(b)
drop v*
simulate b = _b[educ], reps($R ): myboot
sum b
scalar sd = r(sd)
use CARD, clear
label variable educ "Education 1976"
label variable lwage "Log(wage)"
label variable black "Black"
label variable south "Lives in South 1976"
label variable south66 "Lives in South 1966"
label variable smsa "Urban 1976"
label variable smsa66 "Urban 1966"
label variable exper "Experience"
label variable expersq "Experience$^2$"
label variable nearc4 "Lives close to 4 year college 1966"
ivclp_bininstr lwage $controls , endog(educ = nearc4) fm mat(init)
mat Var = e(V)
mat Var[1,1] = sd^2
ConvertVar
outreg2 using Table1.xls, append label noaster nocons ctitle("CLP-FM(2)") addtext(Controls, Yes) dec(4) keep(educ)
capture drop v*

// Davidson and Mackinnon test
ivclp_bininstr lwage $controls , endog(educ = nearc4)
predict y1, xb
drop v*
ivclp_bininstr lwage $controls , endog(educ = nearc4) ifgm
predict y2, xb
drop v*
ivclp_bininstr lwage $controls , endog(educ = nearc4) fm
predict y3, xb
drop v*
save data, replace

mat DM_table = J(3,3,0)
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4) novhat extra(y2)
ereturn display
mat DM_table[1,2] = r(table)["pvalue","y2"]
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , endog(educ = nearc4) novhat extra(y3)
ereturn display
mat DM_table[1,3] = r(table)["pvalue","y3"]


bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , ifgm endog(educ = nearc4) novhat extra(y1)
ereturn display
mat DM_table[2,1] = r(table)["pvalue","y1"]
bootstrap _b, reps($R ): ivclp_bininstr lwage $controls , ifgm endog(educ = nearc4) novhat extra(y3)
ereturn display
mat DM_table[2,3] = r(table)["pvalue","y3"]


simulate b = _b[y1], reps($R ): myboot2 y1
quietly sum b
scalar sd = r(sd)
use data, clear
quietly ivclp_bininstr lwage $controls , endog(educ = nearc4) fm extra(y1) novhat mat(init)
mat Var = e(V)
local loc = colnumb(Var, "y1")
mat Var[`loc',`loc'] = sd^2
ConvertVar
ereturn display
mat DM_table[3,1] = r(table)["pvalue","y1"]
simulate b = _b[y2], reps($R ): myboot2 y2
quietly sum b
scalar sd = r(sd)
use data, clear
quietly ivclp_bininstr lwage $controls , endog(educ = nearc4) fm extra(y2) novhat
mat Var = e(V)
local loc = colnumb(Var, "y2")
mat Var[`loc',`loc'] = sd^2
ConvertVar
ereturn display
mat DM_table[3,2] = r(table)["pvalue","y2"]

putexcel set "Table2.xls", replace
putexcel B1 = "CLP"
putexcel C1 = "CLP-iFGM"
putexcel D1 = "CLP-FM (2)"
putexcel A2 = "CLP"
putexcel A3 = "CLP-iFGM"
putexcel A4 = "CLP-FM (2)"
putexcel B2 = matrix(DM_table)
